package com.cuanbo.dualscreenteachfu.http;

import android.text.TextUtils;

import com.cuanbo.dualscreenteachfu.utils.HttpClient;
import com.cuanbo.dualscreenteachfu.utils.IpUtil;
import com.cuanbo.dualscreenteachfu.utils.LogUtil;
import com.cuanbo.dualscreenteachfu.utils.WebServer;

public class HttpWebServerCheck {
    public void request() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    String result = HttpClient.doGet("http://" + IpUtil.getIpAddressString() + ":8082/heartbeat");
                    //LogUtil.add("检查访问状态", "结果=" + result, "Web服务器");
                    if (TextUtils.isEmpty(result) || !result.trim().equals("ok")) {
                        WebServer.stop();
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        WebServer.start();
                        LogUtil.add("检查访问状态", "结果异常,重启Web服务器！", "Web服务器");
                    }
                }catch (Exception e){
                    LogUtil.addError("--------------------HttpWebServerCheck异常--------------------");
                    LogUtil.addError(e);
                    LogUtil.addError("--------------------------------------------------------------");
                }
            }
        }).start();
    }
}
